# Replication Archive for: 
# Coppock, Alexander and Donald P. Green. 2020. 
# "Do Belief Systems Exhibit Dynamic Constraint?" 
# The Journal of Politics, Forthcoming.

rm(list = ls())

library(tidyverse)
library(readxl)

study_2 <- read_csv("data/raw/study_2_lucid_raw.csv")
study_2_covs <- read_excel("data/raw/study_2_lucid_covariates.xlsx")

study_2 <- study_2[-c(1, 2),]
study_2 <- filter(study_2, !is.na(RID))

study_2 <-
  within(study_2, {
    ideo_4 <- ideo
    ideo_4[ideo %in% c(4, 5)] <- 4
    
    ideo_4 <-
      factor(
        ideo_4,
        1:4,
        labels = c(
          "Liberal",
          "Moderate",
          "Conservative",
          "Other / Prefer not to say"
        )
      )
    
    pid_7 <- rep(NA, nrow(study_2))
    pid_7[pid_3 == 1 & pid_dem == 1] <- 1
    pid_7[pid_3 == 1 & pid_dem == 2] <- 2
    pid_7[pid_3 == 2 & pid_ind == 3] <- 3
    pid_7[pid_3 == 2 & pid_ind == 4] <- 4
    pid_7[pid_3 == 2 & pid_ind == 5] <- 5
    pid_7[pid_3 == 3 & pid_rep == 6] <- 6
    pid_7[pid_3 == 3 & pid_rep == 7] <- 7
    pid_7[is.na(pid_7)] <- 4
    
    
    # religion goes here
    
    trap_set <-
      as.numeric((gov_size == 1 &
                    educ_spend == 1) | (gov_size == 2 & educ_spend == -1))
    
    spend_outcomes_1 <- as.numeric(spend_outcomes_1)
    spend_outcomes_2 <- as.numeric(spend_outcomes_2)
    spend_outcomes_3 <- as.numeric(spend_outcomes_3)
    spend_outcomes_4 <- as.numeric(spend_outcomes_4)
    spend_outcomes_5 <- as.numeric(spend_outcomes_5)
    
    spend_outcomes_average <-
      (
        spend_outcomes_1 +
          spend_outcomes_2 +
          spend_outcomes_3 +
          spend_outcomes_4 +
          spend_outcomes_5
      ) / 5
    
    gays_employers <-
      as.numeric(coalesce(gays_hiring, gays_religious))
    
    welfare <- as.numeric(coalesce(welfare_welfare, welfare_aid))
    
    welfare_good <- -1 * as.numeric(welfare) + 4
    medicaid_good <- as.numeric(medicaid == 1)
    gov_reduce_ineq <- -1 * as.numeric(equiwealth) + 8
    Z_gays = as.numeric(Z_gays)
  })

study_2 <-
  study_2 %>%
  rename(ssm_exempt = sum_exempt) %>%
  mutate(
    ssm_exempt = as.numeric(ssm_exempt == 2),
    ssm_ammend = as.numeric(ssm_ammend),
    prayer_football = as.numeric(prayer_football),
    prayer_ammend = as.numeric(prayer_ammend)
  )

study_2_covs <-
  within(study_2_covs, {
    female <- as.numeric(GENDER == "Female")
    educ_5 <- rep(NA, nrow(study_2_covs))
    EDUCATION <- str_trim(EDUCATION)
    educ_5[EDUCATION %in% c("Middle School - Grades 4 - 8",
                            "Completed some high school")] <- 1
    educ_5[EDUCATION %in% c("High school graduate")] <- 2
    educ_5[EDUCATION %in% c(
      "Completed some college, but no degree",
      "Associate Degree",
      "Other post high school vocational training"
    )] <- 3
    educ_5[EDUCATION %in% c("College Degree (such as B.A., B.S.)",
                            "Completed some graduate, but no degree")] <-
      4
    educ_5[EDUCATION %in% c("Doctorate degree",
                            "Masters degree")] <- 5
    
    

    
    
    
    age_5 <- rep(NA, nrow(study_2_covs))
    age_5[AGE %in% 18:29] <- "18 - 29"
    age_5[AGE %in% 30:39] <- "30 - 39"
    age_5[AGE %in% 40:49] <- "40 - 49"
    age_5[AGE %in% 50:59] <- "50 - 59"
    age_5[AGE >= 60] <- "60+"
    
    race_3 <- rep(NA, nrow(study_2_covs))
    race_3[ETHNICITY == "Black or African American"] <- "Black"
    race_3[ETHNICITY == "White"] <- "White"
    race_3[is.na(race_3)] <- "Other / Prefer not to answer"
    
    RID <- `Response ID`
  })


study_2 <- left_join(study_2, study_2_covs)

study_2 <- within(study_2, {
  educ_5[is.na(educ_5)] <- median(educ_5, na.rm = TRUE)
  
  educ_5 = factor(educ_5, levels = 1:5, 
                  labels = c(
                    "Less than High School",
                    "High School",
                    "Some College",
                    "College",
                    "Graduate School"
                  ))
  
  age_5[is.na(age_5)] <- "60+"
  
  ETHNICITY[is.na(ETHNICITY)] <- "White"
  female[is.na(female)] <- median(female, na.rm = TRUE)
})


write_rds(study_2, "data/clean/study_2_lucid_clean.rds")
